package de.hpi.generator.helpers;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Tools for working with files
 * @author Michael Wolowyk
 */
public class FileHelper {

    public static int aSizeOfByteArray = 1024;
    public static final int aBufferSize = 100000;

    public static void writeTextToFile(String aText, String aFileName, boolean needAppend) throws Exception {
        File aFile = new File(aFileName);
        if (!aFile.exists()) {
            recreateFile(aFileName);
        }
        FileWriter aFileWriter = new FileWriter(aFile, needAppend);
        BufferedWriter aBufferedWriter = new BufferedWriter(aFileWriter);
        aBufferedWriter.write(aText);
        aBufferedWriter.close();
    }

    public static String readTextFromFile(String aFileName) throws IOException {
        FileReader aFileReader = null;
        BufferedReader aBufferedReader = null;
        String result = "";
        aFileReader = new FileReader(new File(aFileName));
        aBufferedReader = new BufferedReader(aFileReader);
        String aTextLine = null;
        while ((aTextLine = aBufferedReader.readLine()) != null) {
            result += aTextLine + "\n";
        }
        aBufferedReader.close();
        return result;
    }

    private static void removeFileOrDirectory(File aFile) throws IOException {
        if (aFile.exists()) {
            if (aFile.isDirectory()) {
                for (File aChildFile : aFile.listFiles()) {
                    removeFileOrDirectory(aChildFile);
                }
            }
            try {

                if (!aFile.delete()) {
                    throw new IOException("The file " + aFile.getPath()
                            + " cannot be deleted");
                }
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        } else {
            throw new IOException("The file " + aFile.getPath()
                    + " is not exists");
        }
    }

    public static void removeFileOrDirectory(String aFileName) throws IOException {
        File aFile = new File(aFileName);
        if (aFile.exists()) {
            removeFileOrDirectory(aFile);
        }
    }

    public static void recreateDirectories(String aFileName) throws IOException {
        File aFile = new File(aFileName);
        if (aFile.exists()) {
            removeFileOrDirectory(aFile);
        }
        if (!aFile.mkdirs()) {
            throw new IOException("a directory " + aFileName
                    + " couldn't be created");
        }
    }

    public static void recreateFile(String aFileName) throws Exception {
        File aFile = new File(aFileName);
        String aParentPath = aFile.getParent();

        if (aFile.exists() && !aFile.delete()) {
            throw new Exception("Cannot delete file:" + aFile.getAbsolutePath());
        }

        if (aParentPath != null && !new File(aParentPath).exists()) {
            recreateDirectories(aParentPath);
        }

        aFile.createNewFile();
    }

    /**
     * returns size of a file with aDataFilePath in bytes
     * 
     * @param aDataFilePath
     *            a path to file
     * @return
     */
    public static int getFileSize(String aDataFilePath) {
        return (int) new File(aDataFilePath).length();
    }

    public static void copyFile(String aSource, String aTarget) throws Exception {
        File inputFile = new File(aSource);
        File outputFile = new File(aTarget);
        if(!(new File(aTarget).exists())){
            recreateFile(aTarget);
        }
        
        BufferedReader in =  new BufferedReader(
                new FileReader(inputFile),
                aBufferSize);
        
        BufferedWriter out = new BufferedWriter(
                new FileWriter(outputFile),
                aBufferSize);
        int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

        in.close();
        out.close();
    }
}